#

| Chapter 25 Showing What's in a File |

## 25.21 Numbering Lines

There are times when you want to print out a file with the lines numbered - perhaps because you are showing a script or program in documentation and want to refer to individual lines in the course of your discussion.

This is one of the handy things cat can do for you, with the -n option.

cat -n precedes each line with some leading spaces, the line number, and a TAB. How many leading spaces? It depends on how high the line numbers go. The line numbers are right justified at column 6, which means that a six-digit number will go all the way back to the margin. I only belabor this point in case you're tempted to trim the leading spaces with something like cut (35.14).

If you have a version of cat that doesn't support -n, try nl, the line-numbering program. nl -ba acts like cat -n. By itself, nl numbers only lines with text.

You can achieve a similar effect with pr -t -n. (The -t keeps pr from inserting the header and footer (43.7) it normally uses to break its output into pages.) And as long as we're giving you choices, here are four more `:-)` from Greg Ubben:

>     grep -n \^
>     awk '{print NR,$0}'
>     sed = | sed 'N;s/\n/ /'
>     ex - '+%#q'

- JP, TOR

25.20 Printing the Top of a File |  | 26. Regular Expressions (Pattern Matching)